package com.collect.senegal.zhiScene.zhiWidget.zhiDialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lihang.ShadowLayout;
import com.collect.senegal.R;

public class ZHIDialogMessage extends Dialog {

    private ImageView imageView;
    private TextView titleTextView;
    private TextView messageTextView;
    private TextView negtiveBtn, positiveBtn;
    private ShadowLayout sl_negtive, sl_positive;

    private String title;
    private String message;
    private SpannableStringBuilder spanMessage;
    private String negtive, positive;
    private int imageResId = -1;
    private int negtiveColor = -1;
    private int positiveColor = -1;
    private boolean isSingleBtn = false;

    private boolean isContentAlignCenter = true;
    private ZHIOnDialogButtonListener onClickButtonListener;

    private Context context;

    public static void showDialog(Context context, String title, String content, boolean hideCancel, ZHIOnDialogButtonListener nextOnDialogButtonListener) {
        ZHIDialogMessage dialogMessage = new ZHIDialogMessage(context);
        dialogMessage.setTitle(title);
        dialogMessage.setMessage(content);
        dialogMessage.setIsSingleBtn(hideCancel);
        dialogMessage.setOnClickButtonListener(nextOnDialogButtonListener);
        dialogMessage.show();
    }

    public static void showDialog(Context context, String title, String content, boolean contentAlignCenter, boolean hideCancel, ZHIOnDialogButtonListener nextOnDialogButtonListener) {
        ZHIDialogMessage dialogMessage = new ZHIDialogMessage(context);
        dialogMessage.setTitle(title);
        dialogMessage.setMessage(content);
        dialogMessage.setIsSingleBtn(hideCancel);
        dialogMessage.setIsContentAlignCenter(contentAlignCenter);
        dialogMessage.setOnClickButtonListener(nextOnDialogButtonListener);
        dialogMessage.show();
    }

    public static void showDialog(Context context, String title, SpannableStringBuilder content, boolean hideCancel, ZHIOnDialogButtonListener nextOnDialogButtonListener) {
        ZHIDialogMessage dialogMessage = new ZHIDialogMessage(context);
        dialogMessage.setTitle(title);
        dialogMessage.setSpanMessage(content);
        dialogMessage.setIsSingleBtn(hideCancel);
        dialogMessage.setOnClickButtonListener(nextOnDialogButtonListener);
        dialogMessage.show();
    }

    public static void showDialog(Context context, String title, String content, String cancel, String confirm, ZHIOnDialogButtonListener nextOnDialogButtonListener) {
        ZHIDialogMessage dialogMessage = new ZHIDialogMessage(context);
        dialogMessage.setTitle(title);
        dialogMessage.setMessage(content);
        dialogMessage.setIsSingleBtn(TextUtils.isEmpty(cancel));
        dialogMessage.setNegtive(cancel);
        dialogMessage.setPositive(confirm);
        dialogMessage.setOnClickButtonListener(nextOnDialogButtonListener);
        dialogMessage.show();
    }

    public ZHIDialogMessage(Context context) {
        super(context, R.style.CustomDialog);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_message);
        setCanceledOnTouchOutside(false);
        initView();
        initViewEvent();
    }

    @Override
    public void show() {
        super.show();
        refreshViewData();
    }

    private void initView() {
        titleTextView = findViewById(R.id.tv_title);
        messageTextView = findViewById(R.id.tv_message);
        imageView = findViewById(R.id.iv_icon);
        negtiveBtn = findViewById(R.id.btn_negtive);
        positiveBtn = findViewById(R.id.btn_positive);
        sl_negtive = findViewById(R.id.sl_negtive);
        sl_positive = findViewById(R.id.sl_positive);
    }

    private void refreshViewData() {
        if (!TextUtils.isEmpty(title)) {
            titleTextView.setText(title);
            titleTextView.setVisibility(View.VISIBLE);
        } else {
            titleTextView.setVisibility(View.GONE);
        }

        if (TextUtils.isEmpty(message) && TextUtils.isEmpty(spanMessage)) {
            messageTextView.setVisibility(View.GONE);

        } else {
            messageTextView.setVisibility(View.VISIBLE);

            if (!TextUtils.isEmpty(message)) {
                messageTextView.setText(message);
                if (isContentAlignCenter) {
                    messageTextView.setGravity(Gravity.CENTER);
                } else {
                    messageTextView.setGravity(Gravity.CENTER_VERTICAL);
                }
            }

            if (!TextUtils.isEmpty(spanMessage)) {
                messageTextView.setText(spanMessage);
            }
        }

        if (-1 == imageResId) {
            imageView.setVisibility(View.GONE);
        } else {
            imageView.setVisibility(View.VISIBLE);
            imageView.setImageResource(imageResId);
        }

        if (-1 != negtiveColor) {
            negtiveBtn.setTextColor(negtiveColor);
        }

        if (-1 != positiveColor) {
            positiveBtn.setTextColor(positiveColor);
        }

        if (!TextUtils.isEmpty(negtive)) {
            negtiveBtn.setText(negtive);
        } else {
            negtiveBtn.setText(context.getResources().getString(R.string.cancel));
        }

        if (!TextUtils.isEmpty(positive)) {
            positiveBtn.setText(positive);
        } else {
            positiveBtn.setText(context.getResources().getString(R.string.confirm));
        }

        if (isSingleBtn) {
            sl_negtive.setVisibility(View.GONE);
            sl_positive.setVisibility(View.VISIBLE);
        } else {
            sl_negtive.setVisibility(View.VISIBLE);
            sl_positive.setVisibility(View.VISIBLE);
        }
    }

    private void initViewEvent() {
        negtiveBtn.setOnClickListener(v -> {
            if (onClickButtonListener != null) {
                onClickButtonListener.onButtonItemClick(0);
            }
            dismiss();
        });

        positiveBtn.setOnClickListener(v -> {
            if (onClickButtonListener != null) {
                onClickButtonListener.onButtonItemClick(1);
            }
            dismiss();
        });
    }

    public ZHIDialogMessage setOnClickButtonListener(ZHIOnDialogButtonListener onClickButtonListener) {
        this.onClickButtonListener = onClickButtonListener;
        return this;
    }

    public ZHIDialogMessage setTitle(String title) {
        this.title = title;
        return this;
    }

    public ZHIDialogMessage setMessage(String message) {
        this.message = message;
        return this;
    }

    public ZHIDialogMessage setSpanMessage(SpannableStringBuilder message) {
        this.spanMessage = message;
        return this;
    }

    public ZHIDialogMessage setNegtive(String negtive) {
        this.negtive = negtive;
        return this;
    }

    public ZHIDialogMessage setNegtiveColor(int color) {
        this.negtiveColor = color;
        return this;
    }

    public ZHIDialogMessage setPositive(String positive) {
        this.positive = positive;
        return this;
    }

    public ZHIDialogMessage setPositiveColor(int color) {
        this.positiveColor = color;
        return this;
    }

    public ZHIDialogMessage setImageResId(int imageResId) {
        this.imageResId = imageResId;
        return this;
    }

    public ZHIDialogMessage setIsSingleBtn(boolean isSingleBtn) {
        this.isSingleBtn = isSingleBtn;
        return this;
    }

    public ZHIDialogMessage setIsContentAlignCenter(boolean isContentAlignCenter) {
        this.isContentAlignCenter = isContentAlignCenter;
        return this;
    }
}
